import { defineStore } from 'pinia'
import getHome from '../api/home'

export const useHomeStore = defineStore('home', {
  state: () => ({
    banner: [],
    brandList: [],
    channel: [],
    couponList: [],
    floorGoodsList: [],
    grouponList: [],
    hotGoodsList: [],
    newGoodsList: [],
    topicList: []
  }), 
  actions: {
    async fetchGetHome() {
      try {
        const res = await getHome()

        if (Object.keys(res).length > 0) {
          let { data } = res
          this.banner = data.banner
          this.brandList = data.brandList
          this.channel = data.channel
          this.couponList = data.couponList
          this.floorGoodsList = data.floorGoodsList
          this.grouponList = data.grouponList
          this.hotGoodsList = data.hotGoodsList
          this.newGoodsList = data.newGoodsList
          this.topicList = data.topicList
        }
        console.log('获取首页数据成功：',res)
      } catch (error) {
        console.error('获取首页数据失败：', error)
      }
    }
  }
})
